package project.ccso.top.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.dao.CityDao;
import project.ccso.top.dao.MemberDao;
import project.ccso.top.dao.ProvinceDao;
import project.ccso.top.dao.SchoolDao;
import project.ccso.top.dao.impl.CityDaoImpl;
import project.ccso.top.dao.impl.MemberDaoImpl;
import project.ccso.top.dao.impl.ProvinceDaoImpl;
import project.ccso.top.dao.impl.SchoolDaoImpl;
import project.ccso.top.entity.Member;
import project.ccso.top.service.MemberService;
import project.ccso.top.util.RandomUtil;
import project.ccso.top.util.SmsUtil;

/**
 * 会员Service的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午11:44:42
 */
public class MemberServiceImpl implements MemberService { 
	// 日志
	private static Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);
	// 注入memberDao
	private MemberDao memberDao = new MemberDaoImpl();
	// 注入provinceDao
	private ProvinceDao provinceDao = new ProvinceDaoImpl();
	// 注入CityDao
	private CityDao cityDao = new CityDaoImpl();
	// 注入SchoolDao
	private SchoolDao schoolDao = new SchoolDaoImpl();
	
	/**
	 * 根据用户电话判断用户是否已经注册
	 * @throws Exception 
	 */
	@Override
	public boolean findByPhoneNumber(String telphone) throws Exception {
		if(StringUtils.isEmpty(telphone)) {
			throw new Exception("参数telphone为NULL或者空字符串！");
		}
		return memberDao.queryByPhoneNumber(telphone);
	}

	/**
	 * 用户注册，将用户信息写入数据库
	 * @throws Exception 
	 */
	@Override
	public boolean addMember(String telphone, String password, String province, String city, String school) throws Exception {
		if(StringUtils.isEmpty(telphone)) {
			throw new Exception("参数telphone为NULL或空字符串！");
		}
		if(StringUtils.isEmpty(password)) {
			throw new Exception("参数password为NULL或空字符串！");
		}
		if(StringUtils.isEmpty(province)) {
			throw new Exception("参数province为NULL或空字符串！");
		}
		if(StringUtils.isEmpty(city)) {
			throw new Exception("参数city为NULL或空字符串！");
		}
		if(StringUtils.isEmpty(school)) {
			throw new Exception("参数school为NULL或空字符串！");
		}
		Member member = new Member(telphone, password, province, city, school);
		member.setNickName("erhuo_" + member.getPhoneNumber()); // 默认昵称为erhuo_+手机号
		member.setPortrait("img/portrait/portrait.png"); // 默认头像
		member.setSex(1); // 性别默认为男
		member.setShieldStatus(0); // 默认为启用状态
		member.setRegTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		member.setAlterTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		return memberDao.addMember(member);
	}

	/**
	 * 用户登录，可以通过用户名和手机号登录
	 * @throws Exception 
	 */
	@Override
	public Member login(String account, String password) throws Exception {
		if(StringUtils.isEmpty(account)) {
			throw new Exception("输入的用户名或手机号为空！");
		}
		if(StringUtils.isEmpty(password)) {
			throw new Exception("输入的密码为空！");
		}
		return memberDao.login(account, password);
	}

	/**
	 * 新用户注册获取短信验证码
	 * @throws Exception 
	 */
	@Override
	public String getSmsCode(String telphone) throws Exception {
		if(StringUtils.isEmpty(telphone)) {
			throw new Exception("参数telphone为NULL或空字符串！");
		}
		// 获取6位随机数
		String code = RandomUtil.getRandomSmscode();
		// 发送验证码短信，并查询详情
		SmsUtil.sendAndQuerySmsCode(telphone, code);
		return code;
	}

	/**
	 * 校验用户输入的短信验证码和产生的验证码是否一致
	 */
	@Override
	public boolean checkSmsCode(String inputSmsCode, String sessionSmsCode) throws Exception {
		boolean flag = false;
		if(StringUtils.isEmpty(inputSmsCode)) {
			throw new Exception("用户输入的短信验证码为空！");
		}
		if(StringUtils.isEmpty(sessionSmsCode)) {
			throw new Exception("session中保存的短信验证码为空！");
		}
		if(sessionSmsCode.equals(inputSmsCode)) {
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 根据用户Id查询用户信息
	 */
	@Override
	public Member findById(Integer id) {
		return memberDao.findById(id);
	}
	/**
	 * 根据用户Id更新用户信息是否成功
	 */
	@Override
	public Boolean updateMessageById(Member user) {
		return memberDao.upadteMessageById(user);
	}
	
	/**
	 * 根据用户id更新用户密码是否成功
	 */
	@Override
	public Boolean updatePasswordById(Member user) {
		return memberDao.upadtePasswordById(user);
	}

	/**
	 * 分页查询用户
	 */
	@Override
	public GridPageBean<Member> findByPage(String pageNoStr, String pageSizeStr, String id, String nickName, String startDate,
			String endDate) throws Exception {
		Integer pageNum = 1;
		Integer pageSize = 10;
		if(!StringUtils.isEmpty(pageNoStr) && !StringUtils.isEmpty(pageSizeStr)){
			boolean flag = true;
			try {
				pageNum = Integer.valueOf(pageNoStr);
				pageSize = Integer.valueOf(pageSizeStr);
			} catch (Exception e) {
				flag = false;
			}
			if(!flag) {
				throw new Exception("参数page或rows不是int类型或者超出int范围！");
			}
		}
		int records = memberDao.countByParams(id, nickName, startDate, endDate);
		GridPageBean<Member> result = new GridPageBean<Member>(pageNum, records, pageSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Member> rows = memberDao.findPageByParams(id, nickName, startDate, endDate, result.getStartIndex(), records);
			result.setRows(rows);
		} else{
			List<Member> rows = memberDao.findPageByParams(id, nickName, startDate, endDate, result.getStartIndex(), result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pageNum);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	/**
	 * 屏蔽或启用用户
	 * @throws Exception 
	 */
	@Override
	public boolean updateShieldStatus(String idStr, String statusStr) throws Exception {
		boolean flag = false;
		int id = 1;
		int transStatus;
		if(StringUtils.isEmpty(idStr)) {
			throw new Exception("参数id为NULL或空字符串！");
		}else {
			try {
				id = Integer.parseInt(idStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				throw new Exception("参数id不是int类型或超出int范围！");
				
			}
		}
		if(StringUtils.isEmpty(statusStr)) {
			throw new Exception("参数transStatus为NULL或空字符串！");
		}else {
			try {
				transStatus = Integer.parseInt(statusStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				throw new Exception("参数transStatus不是int类型或超出int范围！");
			}
		}
		flag = memberDao.updateShieldStatus(id, transStatus);
		return flag;
	}

	/**
	 * 2.验证手机号是否已经注册Service实现类
	 * @return
	 *//*
	@Override
	public boolean findByPhoneNumber(String phoneNumber) {
		return memberDao.findByPhoneNumber(phoneNumber);
	}

	*//**
	 * 3.用户注册Service实现类
	 *//*
	@Override
	public boolean addMember(Member member) {
		// 根据省份id获取到省份信息
		Province province = provinceDao.findProvinceById(Integer.valueOf(member.getProvince()));
		member.setProvince(province.getName());
		// 根据城市id获取到城市信息
		City city = cityDao.findCitysById(Integer.valueOf(member.getCity()));
		member.setCity(city.getName());
		// 根据高校id获取到高校信息
		School school = schoolDao.findSchoolById(Integer.valueOf(member.getSchool()));
		member.setSchool(school.getName());
		// 设置其他的默认信息
		member.setNickName("erhuo_" + member.getPhoneNumber());
		member.setPortrait("img/portrait/portrait.png");
		member.setSex(1);
		member.setShieldStatus(0);
		member.setRegTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		member.setAlterTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		return memberDao.addMember(member);
	}

	*//**
	 * 4.分页查询用户Service实现类
	 *//*
	@Override
	public GridPageBean<Member> findByPage(String pageNo, String pageSize, String id, String nickName, String startDate,
			String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = memberDao.countByParams(id, nickName, startDate, endDate);
		GridPageBean<Member> result = new GridPageBean<Member>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Member> rows = memberDao.findPageByParams(id,nickName,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<Member> rows = memberDao.findPageByParams(id,nickName,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	*//**
	 * 5.根据ID屏蔽用户Service实现类
	 * @throws Exception 
	 *//*
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			memberDao.updateStatus(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			memberDao.updateStatus(id, OpenStatus.SHIELD.getStatusCode());
		}
	}*/
}
